package com.springboot.search.service;

import com.springboot.search.dao.ESGoodsDao;
import com.springboot.search.entity.ESGoods;
import com.springboot.search.esconfig.ESConfig;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ESGoodsService {

    @Autowired
    ESGoodsDao esGoodsDao;
    @Autowired
    ESConfig esConfig;
    /**
     * 查询全部数据
     */
    public List<ESGoods> findAll(){
        Iterable<ESGoods> esGoods=esGoodsDao.findAll();
        List<ESGoods> goods=new ArrayList<>();
        esGoods.forEach(esGood->goods.add(esGood));
       return goods;
    }
    /**
     * 根据输入内容进行搜索
     * @param search
     * @return
     */
    public List<ESGoods> searchEsGoods(String search){
        QueryBuilder builder= QueryBuilders.multiMatchQuery("*"+search+"*","goodsName","goodsNameEn","goodsDescribe");
//        builder.should(QueryBuilders.wildcardQuery("goodsName","*"+search+"*"));
//        builder.should(QueryBuilders.wildcardQuery("goodsNameEn","*"+search+"*"));
//        builder.should(QueryBuilders.wildcardQuery("goodsDescribe","*"+search+"*"));
        System.out.println(builder.toString());
        Page<ESGoods> esGoods= (Page<ESGoods>) esGoodsDao.search(builder);
        return esGoods.getContent();
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    public ESGoods searchESGoodsById(Integer id){
        ESGoods esGoods=null;
        BoolQueryBuilder builder= QueryBuilders.boolQuery();
        builder.must(QueryBuilders.matchQuery("id",id));
        System.out.println(builder.toString());
        Page<ESGoods> data= (Page<ESGoods>) esGoodsDao.search(builder);
        if (data.getContent().size()>0){
            esGoods=data.getContent().get(0);
        }
        return esGoods;
    }

    public List<String> searchESGoodsList(String search)throws Exception{
        SearchRequestBuilder searchRequestBuilder = esConfig.client().prepareSearch("goods");

        //BoolQueryBuilder builder= QueryBuilders.boolQuery();
        QueryBuilder builder= QueryBuilders.multiMatchQuery("*"+search+"*","goodsName");

        //builder.should(QueryBuilders.wildcardQuery("goodsName","*"+search+"*"));
        HighlightBuilder highlightBuilder=new HighlightBuilder()
                .field("goodsName")
                .preTags("<span style='color:red;font-weight:bold;'>").postTags("</span>");
        SearchResponse response=searchRequestBuilder.highlighter(highlightBuilder).setQuery(builder).setPostFilter(builder).get();
        SearchHits hits = response.getHits();
        List<String> goodsName=new ArrayList<>();
        for (SearchHit hit:hits){
            Text[] name=hit.getHighlightFields().get("goodsName").getFragments();
            String goodName="";
            if (name!=null){
                for (Text str:name){
                    goodName+=str;
                }
                goodsName.add(goodName);
            }
        }
        return goodsName;
    }
}
